package com.example.cupid.controller;

import com.example.cupid.pojo.*;
import com.example.cupid.service.Impl.UserServiceImpl;
import com.example.cupid.service.Impl.WechatServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private WechatServiceImpl wechatService;
    @GetMapping
    public String loginPage() {
        return "admin/login";
    }

    @GetMapping("/info")
    public String Info(){return "admin/info";}

    @GetMapping("/userTable")
    public String table(Model model) {
        List<User> list = userService.findAll();
        model.addAttribute("user",list);
        return "admin/table-user";
    }

    @GetMapping("/tableSave")
    public String saveTable(Model model){
        List<UserWechatSava> list = wechatService.getSaveWechat();
        model.addAttribute("userSave",list);
        return "admin/table-user-save";
    }

    @GetMapping("/tableTake")
    public String tableTake(Model model){
        List<UserWechatTake> list = wechatService.getTakeWechat();
        model.addAttribute("userTake",list);
        return "admin/table-user-take";
    }

    @GetMapping("/index")
    public String index(){
        return "admin/index";
    }
    @PostMapping("/login")
    public String login(@RequestParam String email,@RequestParam String password, HttpSession session, RedirectAttributes attributes) {
        Admin admin = userService.checkUser(email, password);
        System.out.println(admin);;
        if (admin != null) {
            session.setAttribute("admin", admin);
            return "/admin/index";
        }
        else {
            attributes.addFlashAttribute("message","用户名或密码错误");
            return "redirect:/admin";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("admin");
        return "redirect:/admin";
    }

}
